package com.ruoyi.bigbase.service.impl;

import java.util.List;

import com.ruoyi.bigbase.service.ICustomerService;
import com.ruoyi.bigbase.service.IIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bigbase.mapper.CustomerMapper;
import com.ruoyi.bigbase.domain.Customer;

/**
 * 客户信息Service业务层处理
 * 
 * @author bigbase
 * @date 2025-05-09
 */
@Service
public class CustomerServiceImpl implements ICustomerService
{
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private IIntegralService integralService;

    /**
     * 查询客户信息
     * 
     * @param id 客户信息主键
     * @return 客户信息
     */
    @Override
    public Customer selectCustomerById(Integer id)
    {
        Customer customer = customerMapper.selectCustomerById(id);

        if (customer != null) {
            customer.setPoints(integralService.getIntegralByUserId(id));
            if(customer.getPoints()==null){
                customer.setPoints(0);
            }
        }


        return customer;
    }

    /**
     * 查询客户信息列表
     * 
     * @param customer 客户信息
     * @return 客户信息
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer)
    {
        List<Customer> customers = customerMapper.selectCustomerList(customer);
        for (Customer c : customers) {
            if (c != null) {
                c.setPoints(integralService.getIntegralByUserId(c.getId()));
                if(c.getPoints()==null){
                    c.setPoints(0);
                }
            }
        }
        return customers;
    }

    /**
     * 新增客户信息
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public int insertCustomer(Customer customer)
    {
        return customerMapper.insertCustomer(customer);
    }

    /**
     * 修改客户信息
     * 
     * @param customer 客户信息
     * @return 结果
     */
    @Override
    public int updateCustomer(Customer customer)
    {
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 批量删除客户信息
     * 
     * @param ids 需要删除的客户信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByIds(Integer[] ids)
    {
        return customerMapper.deleteCustomerByIds(ids);
    }

    /**
     * 删除客户信息信息
     * 
     * @param id 客户信息主键
     * @return 结果
     */
    @Override
    public int deleteCustomerById(Integer id)
    {
        return customerMapper.deleteCustomerById(id);
    }

    @Override
    public Integer getIdByUsername(String userName) {
        return customerMapper.getIdByUsername(userName);
    }

    @Override
    public Customer isUsernameExist(String userName) {
        return customerMapper.isUsernameExist(userName);
    }

    @Override
    public Customer isPhoneExist(String phone) {
        return customerMapper.isPhoneExist(phone);
    }

    @Override
    public Customer isEmailExist(String email) {
        return customerMapper.isEmailExist(email);
    }

}
